﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using UIDesigner.DesignContainers.Bases;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel.Design;
using UIDesigner.Designers.Controls;
using UIDesigner.Editors;

namespace UIDesigner.SurfaceProperties
{
    public class SurfaceActionType
    {
        private SurfaceActionCategory m_eActionCategory;
        private Control m_ctrlPropertyGridView;
        private AFDesignerHost m_objHost;
        private ISelectionService m_objSelectionService;
        private SurfaceActionEditor m_objEditor;
        private IBaseObjectControl m_objControl;

        public SurfacePropertiesBase SurfaceProperties
        {
            get
            {
                return m_objHost.SurfacePropertiesSet[m_objControl];
            }
        }

        public AFDesignerHost Host
        {
            get 
            { 

                return m_objHost; 
            }
        }

        public IBaseObjectControl BaseObjectControl
        {
            get 
            { 
                return m_objControl; 
            }
        }

        public SurfaceActionEditor Editor
        {
            set 
            {
                m_objEditor = value; 
            }
        }

        public Control PropertyGridView
        {
            get 
            { 
                return m_ctrlPropertyGridView; 
            }
        }

        public SurfaceActionCategory ActionCategory
        {
            get 
            { 
                return m_eActionCategory; 
            }
        }

        private void SelectionChanged(object sender, EventArgs e)
        {
            if (m_objEditor != null)
            {
                m_objEditor.RemoveLinks();
            }
        }

        public SurfaceActionType(AFDesignerHost objHost, IBaseObjectControl objControl, SurfaceActionCategory eActionCategory, Control ctrlPropertyGridView)
        {
            m_eActionCategory = eActionCategory;
            m_ctrlPropertyGridView = ctrlPropertyGridView;
            m_objHost = objHost;
            m_objControl = objControl;

            m_objSelectionService = (ISelectionService)((IServiceProvider)m_objHost).GetService(typeof(ISelectionService));

            m_objSelectionService.SelectionChanged += new EventHandler(SelectionChanged);
        }

        public override string ToString()
        {
            return "";
        }
    }

    public class SurfaceActionDescriptor : PropertyDescriptor
    {
        private AFDesignerHost m_objHost;
        private SurfaceActionCategory m_eActionCategory;
        private IBaseObjectControl m_objControl;

        public SurfaceActionDescriptor(AFDesignerHost objHost, IBaseObjectControl objControl, SurfaceActionCategory eCategory, Attribute[] rgAttributes) : base(Enum.GetName(typeof(SurfaceActionCategory), eCategory), rgAttributes)
		{
            m_objHost = objHost;
            m_eActionCategory = eCategory;
            m_objControl = objControl;
		}

        private SurfacePropertiesBase SurfaceProperties
        {
            get
            {
                return m_objHost.SurfacePropertiesSet[m_objControl];
            }
        }

		public override bool CanResetValue(object component)
		{
			return true;
		}

		public override Type ComponentType
		{
			get 
            {
                return m_objHost.RootComponent.GetType(); 
            }
		}

		public override object GetValue(object component)
		{
            switch (m_eActionCategory)
            {
                case SurfaceActionCategory.ApplySavedProperty:
                    return new SurfaceActionType(m_objHost, m_objControl, m_eActionCategory, m_objControl.PropertyGridView);
                case SurfaceActionCategory.RemoveSurfaceControls:
                    return new SurfaceActionType(m_objHost, m_objControl, m_eActionCategory, m_objControl.PropertyGridView);
                default:
                    Debugger.Break();
                    return null;
            }
		}

		public override bool IsReadOnly
		{
			get 
            {
                return false; 
            }
		}

		public override Type PropertyType
		{
			get 
            {
                switch (m_eActionCategory)
                {
                    case SurfaceActionCategory.ApplySavedProperty:
                        return typeof(SurfaceActionType);
                    case SurfaceActionCategory.RemoveSurfaceControls:
                        return typeof(SurfaceActionType);
                    default:
                        Debugger.Break();
                        return null;
                }
            }
		}

		public override void ResetValue(object component)
		{
			
		}

		public override void SetValue(object component, object value)
		{
            if (value == null)
            {
                return;
            }

            switch (m_eActionCategory)
            {
                case SurfaceActionCategory.ApplySavedProperty:
                    break;
                case SurfaceActionCategory.RemoveSurfaceControls:
                    break;
                default:
                    Debugger.Break();
                    break;
            }
        }

		public override bool ShouldSerializeValue(object component)
		{
			return false;
		}

		public override string Category
		{
			get
			{
				return "Surface Properties";
			}
		}

		public override string Description
		{
			get
			{
				return "Shows the hidden field names\r\nAlthough these properties exists at the selected object's type you cannot see them as editable";
			}
		}
    }
}
